package com.xxtx.trade.common.util;


import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类
 *
 * @author pastry
 * @version 1.0
 */
public class DateUtil {

    public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String DATE_FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT_QUERY = "yyyy-MM-dd HH:mm";

    public static final String DATE_FORMAT_DATE = "yyyy年MM月dd日";

    public static final String DATE_FORMAT = "yyyyMMdd";

    public static final String DATE_FORMAT_YYYY_MM2 = "yyyy-MM";

    public static final String DATE_FORMAT_yyyy_MM_dd = "yyyy/MM/dd";

    public static final String DATE_FORMAT_YYYY_MM = "yyyyMM";

    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String DATE_FORMAT_HH_mm_ss = "HH:mm:ss";

    public static final String DATE_FORMAT_YYYY_MM_DD_HH = "yyyy-MM-dd HH";

    public static final String DATE_FORMAT_YYYYMMDDHHmmss = "yyyyMMddHHmmss";

    public static final String DATE_FORMAT_YYYYMMDDHH = "yyyyMMddHH";

    public static final String DATE_FORMAT_NORMAL_CN = "yyyy年MM月dd日HH:mm:ss";

    public static final String DATE_FORMAT_YYYYMMDDHHmmssSSS = "yyyyMMddHHmmssSSS";

    public static final String DATE_FORMAT_FULL_WORLD = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    public static final String TIME_ZERO = ".0";
    /****
     * 联通相关
     */
    /**
     * 获取联通卡的过期时间
     */
    public static Date getCuccCurrentMonthendTime(){
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:55");
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE,26);
            String format = df.format(calendar.getTime());
            return df1.parse(format);
        } catch (Exception e) {

        }
        return new Date();
    }
    public static Date getCuccNextMonthendTime(){
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:55");
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE,26);
            calendar.add(Calendar.MONTH,1);
            String format = df.format(calendar.getTime());
            return df1.parse(format);
        } catch (Exception e) {

        }
        return new Date();
    }
    public static Date getCuccNextMonthendTime(Date date){
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:55");
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DATE,26);
            calendar.add(Calendar.MONTH,1);
            String format = df.format(calendar.getTime());
            return df1.parse(format);
        } catch (Exception e) {

        }
        return new Date();
    }
    public static Date getCucstartNextMonthendTime(){
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE,27);
            calendar.add(Calendar.MONTH,1);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }
    public static Date getCucstartCurrentMonthendTime(){
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE,27);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }

    public static Date getCuccNext2MonthendTime(){
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:55");
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE,26);
            calendar.add(Calendar.MONTH,2  );
            String format = df.format(calendar.getTime());
            return df1.parse(format);
        } catch (Exception e) {

        }
        return new Date();
    }



    /**
     * 一小时，多五分钟
     */
    public static Long DAY_OF_MILLIS_TIME = 24 * 60 * 60 * 1000L + 5 * 60 * 1000L;

    // 用来全局控制 上一周，本周，下一周的周数变化
    private static int weeks = 0;

    /**
     * 将字符串转换为Date日期
     *
     * @param strDate 日期字符串
     * @return
     */
    public static Date stringToDate(String strDate, String formatStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            return format.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将字符串转换为Date日期
     *
     * @param strDate 日期字符串
     * @return
     */
    public static Date worldStrToDate(String strDate, String formatStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            return format.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取传入时间的前一天整点时间戳
     *
     * @param d
     * @return
     */
    public static long getYesterdayTimestamp(Date d) {
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dNow);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取传入时间的前一天,以format显示
     *
     * @param d      日期
     * @param format 日期格式
     * @return
     */
    public static String getYesterdayTimestamp(Date d, String format) {
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dNow);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat formats = new SimpleDateFormat(format);
        return formats.format(new Date(calendar.getTimeInMillis()));
    }

    /**
     * 获取当前月天数
     **/
    public static int getDayOfMonth() {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int day = calendar.getActualMaximum(Calendar.DATE);
        return day;
    }

    /**
     * 获取传入月份天数
     **/
    public static int getDayOfMonth(int month) {

        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, time.get(Calendar.YEAR));
        //year年
        time.set(Calendar.MONTH, month - 1);
        //Calendar对象默认一月为0,month月
        return time.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当前月份
     **/
    public static int getCurrentMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前年份
     **/
    public static int getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取当前几号
     **/
    public static int getCurrentDate() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    /**
     * 判断当前时间是否在时间段内
     *
     * @param startDate   开始时间
     * @param endDate     结束时间
     * @param currentDate 当前时间
     */
    public static boolean isBetweenDate(Date startDate, Date endDate, Date currentDate) {
        if (currentDate.compareTo(startDate) >= 0 && endDate.compareTo(currentDate) >= 0) {
            return true;
        }
        return false;
    }

    public static Long timeDifference(Date d) {
        Date dNow = new Date();
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long between = 0;
        try {
            Date begin = dNow;
            between = (d.getTime() - begin.getTime()) / 1000;// 得到两者的毫秒数
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return between;
    }

    public static Calendar formatRDate(String strDate) {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Calendar cal = Calendar.getInstance();
        try {
            date = sdf2.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(date);
        return cal;
    }

    public static String dateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_FULL);
        return format.format(date);
    }

    public static String currentSSStr(Date d, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(d);
    }

    /**
     * @param date
     * @return yyyy-MM-dd HH:mm
     */
    public static String date2Str(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_QUERY);
        return format.format(date);
    }

    public static String dateToStrNormal(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_NORMAL);
        return format.format(date);
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat dtFormat = null;
        try {
            if (StringUtil.hasText(strDate)) {
                if (strDate.length() == DATE_FORMAT_QUERY.length()) {
                    dtFormat = new SimpleDateFormat(DATE_FORMAT_QUERY);
                } else if (strDate.length() == DATE_FORMAT_YYYY_MM_DD.length()) {
                    dtFormat = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
                } else if (strDate.length() == DATE_FORMAT_NORMAL.length()) {
                    dtFormat = new SimpleDateFormat(DATE_FORMAT_NORMAL);
                } else if (strDate.length() == DATE_FORMAT.length()) {
                    dtFormat = new SimpleDateFormat(DATE_FORMAT);
                } else if (strDate.length() == DATE_FORMAT_HH_mm_ss.length()) {
                    dtFormat = new SimpleDateFormat(DATE_FORMAT_HH_mm_ss);
                }
                return dtFormat.parse(strDate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static java.sql.Date strToSqlDate(String strDate) {
        SimpleDateFormat dtFormat = null;
        try {
            if (strDate.length() == DATE_FORMAT_QUERY.length()) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_QUERY);
            } else if (strDate.length() == DATE_FORMAT_YYYY_MM_DD.length()) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
            } else if (strDate.length() == DATE_FORMAT_NORMAL.length()) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_NORMAL);
            } else if (strDate.length() == DATE_FORMAT.length()) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT);
            } else if (strDate.length() == DATE_FORMAT_HH_mm_ss.length()) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_HH_mm_ss);
            }
            return new java.sql.Date(dtFormat.parse(strDate.trim()).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date stirngToDate(String strDdate, String format) {
        SimpleDateFormat dtFormat = null;

        try {
            if (format.equals(DATE_FORMAT_FULL)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_FULL);
            } else if (format.equals(DATE_FORMAT_NORMAL)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_NORMAL);
            } else if (format.equals(DATE_FORMAT_QUERY)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_QUERY);
            } else if (format.equals(DATE_FORMAT_DATE)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_DATE);
            } else if (format.equals(DATE_FORMAT)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT);
            } else if (format.equals(DATE_FORMAT_YYYY_MM_DD)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
            } else if (format.equals(DATE_FORMAT_YYYYMMDDHHmmss)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_YYYYMMDDHHmmss);
            } else if (format.equals(DATE_FORMAT_yyyy_MM_dd)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_yyyy_MM_dd);
            } else if (format.equals(DATE_FORMAT_FULL_WORLD)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_yyyy_MM_dd);
            } else if (format.equals(DATE_FORMAT_FULL_WORLD)) {
                dtFormat = new SimpleDateFormat(DATE_FORMAT_FULL_WORLD);
            }

            return dtFormat.parse(strDdate);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取时间描述
     */
    public static String getDateSpoken() {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour >= 6 && hour < 8) {
            return "早上";
        } else if (hour >= 8 && hour < 11) {
            return "上午";
        } else if (hour >= 11 && hour < 13) {
            return "中午";
        } else if (hour >= 13 && hour < 18) {
            return "下午";
        } else {
            return "晚上";
        }
    }

    public static String getNowDate() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_DATE);
        return format.format(new Date());
    }


    public static String getNowDate(String dateFormate) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormate);
        return format.format(new Date());
    }
    public static final int month_data=30;
    public static String getNowDay() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return format.format(new Date());
    }
    /**
     * 获取传入时间的后几天
     */
    public static Date getTomorrowDay(Date date,int day,int a) {
        Calendar cd = Calendar.getInstance();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String format1 = format.format(date);
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            cd.setTime(format2.parse(format1));
        }catch (Exception e){

        }
        cd.add(Calendar.DATE, day);
        return cd.getTime();
    }


    /**
     * 获取明天
     */
    public static String getTomorrowDay() {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, 1);
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return format.format(cd.getTime());
    }
    /**
     * 获取传入时间的后几天
     */
    public static Date getTomorrowDay(Date date,int day) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.add(Calendar.DATE, day);
        return cd.getTime();
    }

    /**
     * 获取昨天
     */
    public static Date getYesterday() {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, -1);
        return cd.getTime();
    }


    /**
     * 几小时前
     */
    public static Date getBeforeHour(int hour) {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.HOUR, hour);
        return cd.getTime();
    }

    /**
     * 获取几天前
     * 负数：前几天
     * 证书：后几天
     */
    public static Date getBeforDay(Integer day) {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, day * (-1));
        return cd.getTime();
    }

    /**
     * 获取指定日期的前后日期
     * 负数：前几天
     * 正数：后几天
     */
    public static Date getBeforDayByDate(String strDate, Integer day) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(stirngToDate(strDate, DATE_FORMAT));
        cd.add(Calendar.DATE, day);
        return cd.getTime();
    }

    /**
     * 获取当天凌晨时间
     */
    public static Date getNowDayZero() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        //SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_NORMAL);
        return cal.getTime();
        // return format.format(cal.getTime());
    }

    /**
     * 获取昨天
     */
    public static String getYesterdayStr() {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, -1);
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return format.format(cd.getTime());
    }


    /**
     * 获取几分钟前
     */
    public static Date getBeforeMin(int min) {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.MINUTE, min);
        return cd.getTime();
    }

    /**
     * 获取几分钟前(正为后,负为前)
     */
    public static Date getBeforeMin(int min, Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.add(Calendar.MINUTE, min);
        return cd.getTime();
    }

    /**
     * 获得本年第一天的日期
     */
    public static String getCurrentYearFirstDate() {
        int yearPlus = getYearPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, yearPlus);
        Date yearDay = currentDate.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        // DateFormat df = DateFormat.getDateInstance();
        String preYearDay = format.format(yearDay);
        return preYearDay;
    }

    /**
     * 获取当年的第一天
     *
     * @return
     */
    public static Date getCurrYearFirst() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     *
     * @return
     */
    public static Date getCurrYearLast() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    private static int getYearPlus() {
        Calendar cd = Calendar.getInstance();
        int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 获得当天是一年中的第几天
        cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
        cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。
        int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
        if (yearOfNumber == 1) {
            return -MaxYear;
        } else {
            return 1 - yearOfNumber;
        }
    }

    public static Date getExpireDate(int month) {
        Calendar cd = Calendar.getInstance();
        cd.set(Calendar.MONTH, month);
        return cd.getTime();
    }

    public static String getCNDate(Date lgesSigndate) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_NORMAL_CN);
        return format.format(lgesSigndate);
    }

    public static String strDate2NowDay(String strDate) {
        Date date = strToDate(strDate);
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return format.format(date);
    }

    public static String strDate2NowDay(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return format.format(date);
    }

    // 获得当前日期与本周一相差的天数
    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    // 获得上周星期一的日期
    public static String getPreviousMonday() {
        weeks--;
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
        Date monday = currentDate.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        String preMonday = format.format(monday);
        return preMonday;
    }

    // 获得当前周星期一的日期
    public static String getCurrentMonday() {
        weeks = 0;
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        String preMonday = format.format(monday);
        return preMonday;
    }

    // 获得下周星期一的日期
    public static String getNextMonday() {
        weeks++;
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
        Date monday = currentDate.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        String preMonday = format.format(monday);
        return preMonday;
    }

    /**
     * 返回后几天的日期
     *
     * @param date
     * @param i
     * @return
     */
    public static Date getNextDate(Date date, int i) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime();
    }


    // 获得当周的周日的日期
    public static String getSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
        Date monday = currentDate.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        String preMonday = format.format(monday);
        return preMonday;
    }

    /**
     * 得到某年某月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 得到当年1月的第一天
     *
     * @return
     */
    public static String getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, 1 - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 得到当年12月的最后一天
     *
     * @return
     */
    public static String getLastDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, 12 - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 得到某年某月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }


    /**
     * 得到某年某月的前某个月的月数
     *
     * @param pre 前几个月
     * @return
     */
    public static int getLastDayOfMonth(int pre) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -pre);
        // 因为是0~11月  所以 加1 好是 1~12月
        return (c.get(Calendar.MONTH) + 1);
    }

    /**
     * 得到某年某月的前某个月的月数
     *
     * @return
     */
    public static String getPreMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -4);
        return new SimpleDateFormat("yyyy-MM-01").format(c.getTime());
    }

    /**
     * 获取当前月的第一天
     *
     * @return
     */
    public static String getCurrentMonthFirstDay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -0);
        return new SimpleDateFormat("yyyy-MM-01").format(c.getTime());
    }

    /**
     * 获取当前月的最后一天
     *
     * @return
     */
    public static String getCurrentMonthLastDay() {
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));//设置为1号,当前日期既为本月第一天
        return new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime());
    }

    /**
     * 判断是否是周末
     *
     * @return
     */
    public static boolean isWeekend() {
        Calendar cal = Calendar.getInstance();
        int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 0) {// week ==6 ||  0代表周日，6代表周六
            return true;
        }
        return false;
    }

    /**
     * 获取今天  N年前/后的时间
     *
     * @param years
     * @return
     */
    public static Date todayAfterYear(int years) {
        Calendar calendar = Calendar.getInstance();

        Date date = new Date(System.currentTimeMillis());

        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);

        return date = calendar.getTime();
    }

    /**
     * 获取某天  N年前/后的时间
     *
     * @param years
     * @return
     */
    public static Date thisDayAfterYear(int years, Date date) {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);

        return date = calendar.getTime();
    }

    public static String dateToStr(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }


    /**
     * 获取昨天的16:00
     *
     * @return
     */
    public static long getBeginTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTimeInMillis();
    }

    /**
     * 获取当天的16:00
     *
     * @return
     */
    public static long getEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTimeInMillis();
    }

    public static Date getTodayEnd() {
        return DateUtil.stringToDate(DateUtil.getNowDate(DateUtil.DATE_FORMAT_YYYY_MM_DD + " 23:59:59"), DateUtil.DATE_FORMAT_NORMAL);
    }

    // 获取传入时间的周一
    public static Date getDayMonday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }

    // 获取传入时间的周日
    public static Date getDaySunday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return calendar.getTime();
    }

    // 获取某个时间的周一和周日
    public static Date[] getMondayAndSunday(Date startDate, Date endDate) {
        Date[] dates = new Date[3];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        if (calendar.getTime().after(endDate)) {
            dates[1] = endDate;
        } else {
            dates[1] = calendar.getTime();
        }

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        if (calendar.getTime().before(startDate)) {
            dates[0] = startDate;
        } else {
            dates[0] = calendar.getTime();
        }

        calendar.add(Calendar.WEEK_OF_MONTH, 1);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        dates[2] = calendar.getTime();
        return dates;
    }

    // 获取2个日期天数
    public static int daysOfTwo(Date fDate, Date oDate) {
        if (null == fDate || null == oDate) {
            return -1;
        }
        long intervalMilli = oDate.getTime() - fDate.getTime();
        return (int) (intervalMilli / (24 * 60 * 60 * 1000));
    }


    /**
     * 判断是否为周一
     */
    public static boolean isMonday(String dateStr) {
        Date d = stringToDate(dateStr, DATE_FORMAT_YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 1) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断是否为1号
     */
    public static boolean isMonthNo1(String dateStr) {
        if (StringUtils.isNotBlank(dateStr)) {
            StringBuilder sb = new StringBuilder();
            sb.append(dateStr.substring(0, 8)).append("01");
            if (sb.toString().equals(dateStr)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取当前日期上一个月日期
     */
    public static String getLastMonthDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd");
        return formats.format(new Date(calendar.getTimeInMillis()));
    }

    /**
     * 格式化传入的日期
     *
     * @param d      日期
     * @param format 日期格式
     * @return
     */
    public static String formatDate(String d, String format) {
        SimpleDateFormat formats = new SimpleDateFormat(DATE_FORMAT_NORMAL);
        Date date = null;
        try {
            date = formats.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat(format).format(date);
    }

    /*--------时间戳方法start--------*/

    /**
     * 获取当前时间到1970.1.1的秒数
     */
    public static long getCurrSeconds() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 返回从1970年1月1日到现在的总毫秒数 ,时间戳
     */
    public static long getCurrMiliseconds() {
        return System.currentTimeMillis();
    }

    /**
     * 返回Date类型，milisecond,毫秒
     */
    public static Date getDate(long milisecond) throws ParseException {
        SimpleDateFormat formats = new SimpleDateFormat(DATE_FORMAT_NORMAL);
        String d = formats.format(milisecond);
        Date date = formats.parse(d);
        return date;

    }

    /**
     * 返回Date类型，milisecond,毫秒
     */
    public static Date getDateByTime(long milisecond) throws ParseException {
        SimpleDateFormat formats = new SimpleDateFormat(DATE_FORMAT_YYYYMMDDHHmmssSSS);
        String d = formats.format(milisecond);
        Date date = formats.parse(d);
        return date;

    }

    /**
     * <br>获取当天剩余的秒数</br>
     *
     * @return
     * @author 515120
     * @email yk
     * @date 2017年6月19日 下午7:37:13
     * @version 1.0
     * @since 1.0
     */
    public static int getSecondsRemainingofNowDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.getTimeInMillis();
        long secondsRemaining = (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
        return (int) secondsRemaining;
    }

    /**
     * <br>TODO(获取响应时间)</br>
     *
     * @param start
     * @param end
     * @return
     * @author 516649
     * @email yk
     * @date 2017年8月21日 上午11:18:52
     * @version 1.0
     * @since 1.0
     */
    public static String getSpendTime(long start, long end) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(end - start);
        String spendTime = String.format("%s分%s秒%s毫秒", c.get(Calendar.MINUTE), c.get(Calendar.SECOND), c.get(Calendar.MILLISECOND));
        return spendTime;
    }

    /**
     * 时间戳是否在24小时之内
     *
     * @param millisTime
     * @return
     */
    public static Boolean isIn24Hour(Long millisTime) {
        Long currentTime = System.currentTimeMillis();
        return currentTime - millisTime <= DAY_OF_MILLIS_TIME;
    }

    /**
     * 标准化时间显示
     * yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @return
     */
    public static String formatDate1(String dateStr) {
        String[] aStrings = dateStr.split(" ");
        // 5
        if (aStrings[1].equals("Jan")) {
            aStrings[1] = "01";
        }
        if (aStrings[1].equals("Feb")) {
            aStrings[1] = "02";
        }
        if (aStrings[1].equals("Mar")) {
            aStrings[1] = "03";
        }
        if (aStrings[1].equals("Apr")) {
            aStrings[1] = "04";
        }
        if (aStrings[1].equals("May")) {
            aStrings[1] = "05";
        }
        if (aStrings[1].equals("Jun")) {
            aStrings[1] = "06";
        }
        if (aStrings[1].equals("Jul")) {
            aStrings[1] = "07";
        }
        if (aStrings[1].equals("Aug")) {
            aStrings[1] = "08";
        }
        if (aStrings[1].equals("Sep")) {
            aStrings[1] = "09";
        }
        if (aStrings[1].equals("Oct")) {
            aStrings[1] = "10";
        }
        if (aStrings[1].equals("Nov")) {
            aStrings[1] = "11";
        }
        if (aStrings[1].equals("Dec")) {
            aStrings[1] = "12";
        }
        return aStrings[5] + "-" + aStrings[1] + "-" + aStrings[2] + " " + aStrings[3];
    }

    /**
     * 返回时间戳
     */
    public static long getMiliseconds(Date date) {
        return date.getTime();
    }
    /*--------时间戳方法end--------*/


    /**
     * 格式化为date类型的
     */

    public static Date formatDate2(String dateStr) {
        String[] aStrings = dateStr.split(" ");
        // 5
        if (aStrings[1].equals("Jan")) {
            aStrings[1] = "01";
        }
        if (aStrings[1].equals("Feb")) {
            aStrings[1] = "02";
        }
        if (aStrings[1].equals("Mar")) {
            aStrings[1] = "03";
        }
        if (aStrings[1].equals("Apr")) {
            aStrings[1] = "04";
        }
        if (aStrings[1].equals("May")) {
            aStrings[1] = "05";
        }
        if (aStrings[1].equals("Jun")) {
            aStrings[1] = "06";
        }
        if (aStrings[1].equals("Jul")) {
            aStrings[1] = "07";
        }
        if (aStrings[1].equals("Aug")) {
            aStrings[1] = "08";
        }
        if (aStrings[1].equals("Sep")) {
            aStrings[1] = "09";
        }
        if (aStrings[1].equals("Oct")) {
            aStrings[1] = "10";
        }
        if (aStrings[1].equals("Nov")) {
            aStrings[1] = "11";
        }
        if (aStrings[1].equals("Dec")) {
            aStrings[1] = "12";
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = aStrings[5] + "-" + aStrings[1] + "-" + aStrings[2] + " " + aStrings[3];
        Date datetime = null;
        try {
            datetime = df.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return datetime;
    }

    //**  =========================  以下方法by Jay.Wang (用于获取套餐生效时间) ======================= **/

    /**
     * 获取下个月的当前时间
     */
    public static Date getCurrentMonthNextMonthTime() {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.MILLISECOND,0);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 获取当前时间多少天后的时间
     */
       public static Date plusDay(int num){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        return calendar.getTime();
       }

    /**
     * 获取本月的最后一天时间
     */
    public static Date getMonthEndDayTime() {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,0);
            //创建当前时间
            Date date = new Date();
             //设置时间格式为yyyy-MM-dd HH:mm:ss
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //设置当前时间,
            calendar.setTime(date);

            //打印当前时间
            System.out.println("当前时间：" + sdf.format(date));
            //获取到本月起始日
            int actualMinimum = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
            //获取到本月结束日
            int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

            calendar.set(Calendar.MILLISECOND,0);

            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY),actualMinimum,00,00,00);
                   //打印本月起始日的年月日时分秒格式
            System.out.println("这个月的第一天是： " + sdf.format(calendar.getTime()));
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY),actualMaximum,23,59,59);
            //打印本月结束日的年月日时分秒格式
            System.out.println("这个月的最后一天是： " + sdf.format(calendar.getTime()));
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 获取下个月的第一天时间
     */
    public static Date getNextMonthFirstDayTime() {
        try {
            Calendar calendar = Calendar.getInstance();
            // 添加1个月
            calendar.add(Calendar.MONTH, 1);
            int minDay = calendar.getMinimum(Calendar.DAY_OF_MONTH);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), minDay, 0, 0, 0);
            calendar.set(Calendar.MILLISECOND,0);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 获取下个月的最后一天时间
     */
    public static Date getNextMonthEndDayTime() {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, 2);
            int minDay = calendar.getMinimum(Calendar.DAY_OF_MONTH);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), minDay, 0, 0, 0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.setTimeInMillis(calendar.getTimeInMillis() - 1000);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }


    /**
     * 获取顺延月份的下一个月第一天时间
     */
    public static Date getNextMonthFirstDayTimeByTime(Date date) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 添加1个月
            calendar.add(Calendar.MONTH, 1);
            int minDay = calendar.getMinimum(Calendar.DAY_OF_MONTH);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), minDay, 0, 0, 0);
            calendar.set(Calendar.MILLISECOND,0);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 获取顺延月份的下一个月最后一天时间
     */
    public static Date getNextMonthEndDayTimeByTime(Date date) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 2);
            int minDay = calendar.getMinimum(Calendar.DAY_OF_MONTH);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), minDay, 0, 0, 0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.setTimeInMillis(calendar.getTimeInMillis() - 1000);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> test(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i));
            fetureDaysList.add(getFetureDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    public static String currMonthStr(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM2);
        return format.format(calendar.getTime());
    }

    public static String preMonthStr(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YYYY_MM2);
        return format.format(calendar.getTime());
    }

    /**
     * 获取前一个月最后一天
     *
     * @return
     */
    public static Date getPrefixMonthEndDayTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    public static Date getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }


    /**
     * 获取本月第一天
     */
    public static String currMonthFirstDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        int minDay = calendar.getMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), minDay, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND,0);
        return dateToStrNormal(calendar.getTime());
    }

    /**
     * 获取本月最后一天
     */
    public static Date currMonthLastDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        int minDay = calendar.getMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), minDay, 0, 0, 0);
        calendar.setTimeInMillis(calendar.getTimeInMillis() - 1000);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

   
}